package com.avit.tv.kui.progressbar;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.avit.tv.kui.AvitPartChildMarqueeView;
import com.avit.tv.kui.background.BgDrawable;
import com.avit.tv.kui.base.BaseFocusLayout;

import java.util.List;

/**
 * progress 底部标签view
 *
 * @author like
 * @date 2020/7/23
 */
public class ProgressTagBar extends BaseFocusLayout {
    /**
     * tag 背景色
     */
    private Drawable drawable;

    /**
     * 外部显示的点的位置
     */
    private List<ImportPointValue> points;


    @ColorInt
    private int textColor;

    private int textSize;

    private LayoutParams textParams;

    private Rect tagRect;

    /**
     * 外部实际的宽度
     */
    private int measuredWidth;

    /**
     * 背景颜色
     */
    private int bgcolor;

    public ProgressTagBar(@NonNull Context context) {
        super(context);
    }

    public ProgressTagBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressTagBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        textParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textColor = Color.WHITE;
        textSize = DEFAULT_SIZE;
        bgcolor = Color.DKGRAY;
    }

    private AvitPartChildMarqueeView initTextView() {
        AvitPartChildMarqueeView textView = new AvitPartChildMarqueeView(getContext());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setTextColor(textColor);
        textView.setFocused(true);
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(textParams);
        textView.makeFocusable(false);
        return textView;
    }

    @Override
    protected void initContent(Context context) {
        drawable = new BgDrawable().setBgColor(bgcolor).setRadius(0);
        tagRect = new Rect();
        setBackground(drawable);
    }

    @Override
    protected void insertView() {
        loseFocus(true);
    }

    public void initTextValues(List<ImportPointValue> values, int measuredWidth){
        this.measuredWidth = measuredWidth;
        removeAllViewsInLayout();
        for (int i = 0 ; i<values.size() ; i++){
            ImportPointValue value = values.get(i);
            Long pointTime = value.getPointStartTime();
            String pointValue = value.getPointValue();
            AvitPartChildMarqueeView channelView = initTextView();
            channelView.setText(pointValue);
            channelView.setTag(pointValue);
            addView(channelView);
            channelView.setVisibility(GONE);
        }
    }

    private void reduceText() {
        if (points == null || points.isEmpty()) return;
        for (int i = 0; i < getChildCount(); i++) {
            AvitPartChildMarqueeView view = (AvitPartChildMarqueeView) getChildAt(i);
            if (checkIsRight(view)!=0){
                view.setVisibility(VISIBLE);
            }else {
                view.setVisibility(GONE);
            }
        }
    }

    private int checkIsRight(AvitPartChildMarqueeView view){
        Object tag = view.getTag();
        int tempFlag = 0;
        for (int i = 0; i< points.size(); i++){
            ImportPointValue value = points.get(i);
            if (tag instanceof String && tag.equals(value.getPointValue())){
                tempFlag = 1;
                view.setX(value.getTagTextX());
                view.setY(textSize);
                calculateMaxWidth(view,i);
                break;
            }
        }
        return tempFlag;
    }

    private void calculateMaxWidth(AvitPartChildMarqueeView view, int position){
        ImportPointValue nowValue = points.get(position);
        if (position+1 < points.size()){
            ImportPointValue otherValue = points.get(position + 1);
            float textWidth = otherValue.getTagTextX() - nowValue.getTagTextX();
            view.setWidth(Math.round(textWidth));
        }else {
            float textWidth = measuredWidth - nowValue.getTagTextX();
            view.setWidth(Math.round(textWidth));
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        tagRect.left = left;
        tagRect.right = right;
        tagRect.top = top;
        tagRect.bottom = bottom;
    }


    public void setDrawableColor(@ColorInt int color){
        this.bgcolor = color;
        drawable = new BgDrawable().setBgColor(bgcolor).setRadius(0);
        setBackground(drawable);
    }

    /**
     * 设置文字颜色
     * @param textColor
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    /**
     * 设置显示大小
     * @param textSize
     */
    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setPoints(List<ImportPointValue> points) {
        this.points = points;
        reduceText();
    }

}
